<?php
namespace logic\sys\safe;
/**
 * 输入敏感词过滤
 */
class InputFilter{
    
    private $filterListSave; // 需要保存的过滤日志
    private $filterList;    // 过滤记录
    private $SensitiveHelper;
    private $group = 'def'; // 对过滤的数据进行过滤
    private $level = ['politics'=> 1,'porn' => 2,'other' => 3];
    private function __construct(){
        $this->SensitiveHelper = \util\DfaFilter\SensitiveHelper::init();
    }
    
    /**
     * 获取单例对象
     */
    public static function getInstance(){
        static $_instance	= '';
		if(! $_instance ){
            $_instance = new Static();
		}
		return $_instance;
    }
    
    /**
     * 过滤的内容数组
     * @param $content 过滤的内容
     * @param $filterKey 过滤内容中指定的key
     * @param $replaceChar 过滤敏感词替换的内容
     * @param $level 敏感词过滤级别
     */
    public function replace($table,$content,$filterKey='',$replaceChar = '*',$level = null){
        $this->group = $table;
        if( is_null($level)) $level = array_keys($this->level);
        if( !$content ) return $content;
        if( is_string($content)){
            $content = $this->dealReplace($content,$replaceChar,$level);
        }         
        if( is_array( $content )){
            $filterKey = $this->dealFilterKey($filterKey);
            foreach( $filterKey as $key => $val ){
                if(isset($content[$val]))   $content[$val] = $this->dealReplace($content[$val],$replaceChar,$level,$val);
            }
        }
        return $content;
    }
    
    /**
     * 对当前数据进行分组设计
     */    
    private function group($group){
        $this->group = $group;
        return $this;
    }
    
    /**
     * 预保存当前的过滤结果
     * @param $table 过滤模块标识 建议 数据表名称
     * @param $tableid 过滤数据ID
     * @param $group 如果一个逻辑存在多个表过滤时 进行分组区别
     */
    public function save($table,$tableid = 0){
       // $group = $group == '' ? $this->group : $group;
        $group = $table;
        $badFilterList = $this->filterList[$group];
        if( $badFilterList ){
            $resultList = [];
            foreach( $badFilterList as $key => $val ){
                $resultList[$val['tablefield']] .= $this->level[$val['filtertype']].":".$val['filterword'] . ' ' ;
            }
            $result = '';
            foreach( $resultList as $skey => $sval ){
                $result .= '['.$skey. '  ' .$sval .' ] ';
            }
            unset($this->filterList[$group]);
            $this->filterListSave[] = [
                'filter'    => $result,
                'table'     => $table,
                'tableid'   => $tableid
            ];
        }
        return true;
    }
    
    /**
     * 获取当前过滤的敏感词词
     */
    public function getFilterList($table=''){
        if( $table) return  $this->filterList[$table];
        return $this->filterList;
    }
    
    
    
    /**
     * 处理过滤的key
     */
    private function dealFilterKey($filterKey){        
        $result = [];
        if( is_array($filterKey) ){
            foreach( $filterKey as $key => $val ){                
                if( is_numeric($key) ) return $filterKey;
                if( is_array($val) ){
                    if( isset($val['filter']) ) $result[] = $key;
                }
            }
        }    
        return $result;
    }
    
    /**
     * 执行过滤
     */
    private function dealReplace($content,$replaceChar = '*',$level='',$tablefield = ''){
        foreach( $level as $key => $filterFile ){
            $content = $this->SensitiveHelper->setTreeByFile($filterFile)->replace($content,$replaceChar);
            $badFilter =  $this->SensitiveHelper->getBadWordList();
            if( $badFilter ){
                $this->filterList[$this->group][] = [
                    'tablefield' => $tablefield,
                    'filtertype' => $filterFile,
                    'filterword' => implode(',',$badFilter)
                ];
            }
        }
        return $content;        
    }
    
    
    public function __destruct(){
        if( !$this->filterListSave ) return;
        $Passport = \logic\user\Passport::getInstance();
        $CallUser = $Passport->checkLogin();
        if( $CallUser ){
            $userId = $CallUser->userId;
            $role   = $CallUser->role;
        }
        $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        
        foreach( $this->filterListSave as $key => $val ){
            $data = [
                'userrole' => $role ?: '',
                'userid' => $userId ?: 0,
                'table'  => $val['table'],
                'tableid'=> $val['tableid'],
                'filter' => $val['filter'],
                'url'    => $url
            ];
            D('Filterlog','sysSafe')->add($data);
        }
        
    }
}